#include"inputelementstream.h"
#include<errno.h>
#include<fcntl.h>
#include<iostream>
#include<cstdio>
#include<cstring>

bool inputelementstream::end_of_stream()
{
	 int* num = new int;
	 if(read(this->fd, num, sizeof(int)) == 0)
	 	return true;
	 else
	        lseek(this->fd, -1*sizeof(int), SEEK_CUR);
	 return false;
}

void inputelementstream::open_s(const char *fileName)
{
	this->fd = open(fileName, O_RDONLY);
	if(this->fd == -1)
                std::cout << "Error opening stream: " << strerror( errno ) << std::endl;
}

int inputelementstream::read_next()
{
	int* num = new int;
	if(!end_of_stream())
	{
		if(read(this->fd, num, sizeof(int)) < 0)
			std::cout << "Error reading from stream: " << strerror( errno ) << std::endl;
		int data = (*num);
		delete num;
		return data;
	}
	return EOF;
}

void inputelementstream::close_i()
{
	int ret = close(this->fd);
	if(ret < 0)
	          std::cout << "Error closing stream: " << strerror( errno ) << std::endl;
}

